'use strict'

import axios from 'axios'

let watermark = {}

const pageConfig = require('../store/config')

let setWatermark = (str) => {
  let id = '1.23452384164.123412415'

  if (document.getElementById(id) !== null) {
    document.body.removeChild(document.getElementById(id))
  }

  let can = document.createElement('canvas')
  can.width = document.documentElement.clientWidth / 2
  can.height = document.documentElement.clientWidth / 2 + 20

  let cans = can.getContext('2d')
  // cans.strokeStyle = 'red'
  // cans.lineWidth = 1
  // cans.strokeRect(0, 0, can.width, can.width)
  cans.rotate(45 * Math.PI / 180)
  cans.font = '17px PingFang SC'
  cans.fillStyle = 'rgba(0, 0, 0, 0.07)'
  cans.fillText(str, 10, 10)

  let div = document.createElement('div')
  div.id = id
  div.style.pointerEvents = 'none'
  div.style.top = '65px'
  div.style.left = '25px'
  div.style.position = 'fixed'
  div.style.zIndex = '10000000'
  div.style.width = document.documentElement.clientWidth - 25 + 'px'
  div.style.height = document.documentElement.clientHeight - 65 + 'px'
  div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat'
  document.body.appendChild(div)
  return id
}

// 该方法只允许调用一次
watermark.set = () => {
  let data = sessionStorage.getItem('WaterMark') ? JSON.parse(sessionStorage.getItem('WaterMark')) : null;
  if (data) {
    let id = setWatermark(data.name + ' ' + data.mobile)
    setInterval(() => {
      if (document.getElementById(id) === null) {
        id = setWatermark(data.name + ' ' + data.mobile)
      }
    }, 500)
    window.onresize = () => {
      setWatermark(data.name + ' ' + data.mobile)
    }
  }

  // axios({
  //   method: 'POST',
  //   url: 'etc-analysis/wxSign/getWxUser',
  //   data: {
  //     code: sessionStorage.getItem('code') || '9aDG85L3x5G_ELRZedGeoSFmmL5qSOR0alSDrJdwe8A',
  //     appId: pageConfig.appid,
  //     corpId: '1000097'
  //   },
  //   headers: {
  //     'Content-Type': 'application/json'
  //   },
  //   responseType: 'json',
  //   baseURL: 'https://zytest.ln-gst.com/ziyanapi/api/'
  // }).then(function (res) {
  //   if (res.data.code === 200) {
  //     let id = setWatermark(res.data.data.name + ' ' + res.data.data.mobile)
  //     setInterval(() => {
  //       if (document.getElementById(id) === null) {
  //         id = setWatermark(res.data.data.name + ' ' + res.data.data.mobile)
  //       }
  //     }, 500)
  //     window.onresize = () => {
  //       setWatermark(res.data.data.name + ' ' + res.data.data.mobile)
  //     }
  //   }
  // }).catch(function (err) {
  //   // sessionStorage.removeItem('parameterCode');
  //   // sessionStorage.removeItem('code');
  //   // console.log(err)
  // })
}

export default watermark
